package com.cjh.lms.service;

import java.util.List;
import java.util.Random;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.cjh.lms.json.AppResponse;
import com.cjh.lms.mapper.TLockEnterMapper;
import com.cjh.lms.mapper.TLockInfoMapper;
import com.cjh.lms.mapper.TLockTypeMapper;
import com.cjh.lms.pojo.TLockInfo;
import com.cjh.lms.pojo.TLockInfoExample;

@Service
public class AppService {
	
	@Autowired
	TLockInfoMapper tLockInfoMapper;
	@Autowired
	TLockTypeMapper tLockTypeMapper;
	@Autowired
	TLockEnterMapper tLockEnterMapper;
	
	//锁具入库
	@Transactional
	public AppResponse appEnterLock(String lockNo,String cipher){
		AppResponse appResult = new AppResponse();
//		//判断锁型号是否存在，不存在则添加
//		String tCode = null;
//		TLockTypeExample tLockTypeExample = new TLockTypeExample();
//		TLockTypeExample.Criteria  criteria = tLockTypeExample.createCriteria();
//		criteria.andLocTypeEqualTo(lockType);
//		List<TLockType> tLockTypes = tLockTypeMapper.selectByExample(tLockTypeExample);
//		if(tLockTypes.size()==0){
//			TLockType tLockType = new TLockType();
//			tCode = UUID.randomUUID().toString().replace("-", "");
//			tLockType.settCode(tCode);
//			if(tLockTypeMapper.insert(tLockType)<1){
//				JSONObject jsonObject = new JSONObject();
//				jsonObject.put("errCode", 1);
//				jsonObject.put("state", 10001);
//				return jsonObject;
//			}
//		}else{
//			tCode = tLockTypes.get(0).gettCode(); 
//		}
		
		TLockInfoExample tLockInfoExample = new TLockInfoExample();
		TLockInfoExample.Criteria criteria1 = tLockInfoExample.createCriteria();
		criteria1.andLockNoEqualTo(lockNo);
		List<TLockInfo> a = tLockInfoMapper.selectByExample(tLockInfoExample);
		if(a.size()==0){
			//生成入库记录
//			Calendar calendar=Calendar.getInstance();
//			Date eDate = calendar.getTime();
//			String eCode = UUID.randomUUID().toString().replace("-", "");
//			TLockEnter tLockEnter = new TLockEnter();
//			tLockEnter.seteCode(eCode);
//			tLockEnter.seteDate(eDate);
//			tLockEnter.seteNum(1);
//			tLockEnter.settCode(tCode);
//			tLockEnter.setuCode("2642e5a12b3246d5b487b9f182dd47a3");
//			if(tLockEnterMapper.insertSelective(tLockEnter)<1){
//				JSONObject jsonObject = new JSONObject();
//				jsonObject.put("errCode", 1);
//				jsonObject.put("state", 10002);
//				return jsonObject;
//			}
			//锁具信息入库
			TLockInfo tLockInfo = new TLockInfo();
			tLockInfo.setOldInitkey("12345678");
			tLockInfo.setOldSesskey("87654321");
			tLockInfo.setLockNo(lockNo);
			tLockInfo.setlCode(UUID.randomUUID().toString().replace("-", ""));
			tLockInfo.seteCode("");
			tLockInfo.setState(0);
			String newInitkey = getRandomString(8);
			String newSesskey = getRandomString(8);
			tLockInfo.setNewInitkey(newInitkey);
			tLockInfo.setNewSesskey(newSesskey);
			if(tLockInfoMapper.insertSelective(tLockInfo)<1){
				appResult.putResult(01, 10001);//数据库入库失败
				return appResult;
			}
			appResult.putEncrypt("newInitkey", newInitkey);
			appResult.putEncrypt("newSesskey", newSesskey);
			appResult.putEncrypt("cipher", cipher);
			appResult.putSuccessResult();
			return appResult;
		}else{
			TLockInfo tLockInfo = a.get(0);
			String oldInitkey = tLockInfo.getNewInitkey();
			String oldSesskey = tLockInfo.getNewSesskey();
			String newInitkey = getRandomString(8);
			String newSesskey = getRandomString(8);
			tLockInfo.setNewInitkey(newInitkey);
			tLockInfo.setNewSesskey(newSesskey);
			tLockInfo.setOldInitkey(oldInitkey);
			tLockInfo.setOldSesskey(oldSesskey);
			if(tLockInfoMapper.updateByPrimaryKey(tLockInfo)<1){
				appResult.putResult(01, 10001);//数据库操作	失败
				return appResult;
			}
			appResult.putEncrypt("newInitkey", newInitkey);
			appResult.putEncrypt("newSesskey", newSesskey);
			appResult.putEncrypt("cipher", cipher);
			appResult.putSuccessResult();
			return appResult;
		}
	}
	
	
	public AppResponse rollBackData(String lockNo,String cipher){
		AppResponse appResult = new AppResponse();
		TLockInfoExample tLockInfoExample = new TLockInfoExample();
		TLockInfoExample.Criteria criteria1 = tLockInfoExample.createCriteria();
		criteria1.andLockNoEqualTo(lockNo);
		List<TLockInfo> a = tLockInfoMapper.selectByExample(tLockInfoExample);
		TLockInfo tLockInfo = a.get(0);
		tLockInfo.setNewInitkey(tLockInfo.getOldInitkey());
		tLockInfo.setNewSesskey(tLockInfo.getOldSesskey());
		if(tLockInfoMapper.updateByPrimaryKey(tLockInfo)<1){
			appResult.putResult(01, 10001);//数据库入库失败
			return appResult;
		}
		return appResult.putSuccessResult();
	}
	
	//生成8位随机字符
	private static String getRandomString(int length) { //length表示生成字符串的长度  
        String base = "abcdef0123456789";     
        Random random = new Random();     
        StringBuffer sb = new StringBuffer();     
        for (int i = 0; i < length; i++) {     
            int number = random.nextInt(base.length());     
            sb.append(base.charAt(number));     
        }     
        return sb.toString();     
     }
	
}
